package controllers

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"strconv"
	"testGin/model/mysql"
	"testGin/model/tool"
)

type RoleController struct{}

func (ctr RoleController) AuthAjax(c *gin.Context) {

	rid := c.PostForm("rid")
	ids,_ := c.GetPostFormArray("ids[]")

	ridInt,_:=strconv.Atoi(rid)

    mysql.AdminNodeItem{}.DelByRid(ridInt)

	for _, id := range ids {

		idInt,_ := strconv.Atoi(id)
		temp := mysql.AdminNodeItem{}
		temp.IdNodes = uint(idInt)
		temp.IdRole = uint(ridInt)
		temp.Status = 1

		fmt.Println("temp",temp)
		mysql.AdminNodeItem{}.Add(temp)
	}

	tool.Success(c, 200, "请求成功","")

}
func (ctr RoleController) Auth(c *gin.Context) {

	rid := c.Query("rid")
	id := c.Query("id")
	ridInt, _ := strconv.Atoi(rid)
	if ridInt > 0 {
		ss := mysql.Node{}.GetNodeListByRoldId(ridInt)
		tool.SuccessAdmin(c, 200, "请求成功", 0, ss)
	} else {
		idInt, _ := strconv.Atoi(id)
		rItem, err := mysql.Role{}.GetItemById(idInt)
		if err != nil {
			tool.Error(c, 0, "当前角色不存在")
			return
		}
		c.HTML(200, "admin/role/auth.html", gin.H{
			"item": rItem,
			"rid":  id,
		})
	}

}

func (ctr RoleController) Index(c *gin.Context) {
	c.HTML(200, "admin/role/index.html", gin.H{})
}
func (ctr RoleController) GetList(c *gin.Context) {

	page := c.DefaultQuery("page", "1")
	limit := c.DefaultQuery("limit", "15")
	where := []map[string]interface{}{}
	pageInt, err := strconv.Atoi(page)

	if err != nil {
		tool.Error(c, 1, err.Error())
	}

	limitInt, err := strconv.Atoi(limit)

	if err != nil {
		tool.Error(c, 1, err.Error())
	}

	ctrinId := c.Query("ctrinId")
	if ctrinId != "" {
		where = tool.MapWhere("id_ctrin", "=", ctrinId)
	}
	account := c.DefaultQuery("account", "")
	if account != "" {
		where = tool.MapWhere("account", "=", account)
	}
	name := c.DefaultQuery("name", "")
	if name != "" {
		where = tool.MapWhere("name", "=", name)
	}
	result, err := mysql.Role{}.GetList(where, pageInt, limitInt)
	if err != nil {
		tool.Error(c, 1, err.Error())
		return
	}
	tool.SuccessAdmin(c, 0, "请求成功", result["count"].(int), result["data"])
}

func (ctr RoleController) Add(c *gin.Context) {

	c.HTML(200, "admin/role/add.html", "")
}

func (ctr RoleController) AddAjax(c *gin.Context) {

	role := mysql.Role{}

	if err := c.Bind(&role); err != nil {
		tool.Error(c, 0, err.Error())
		return
	}
	role.Status = 1
	errs := mysql.Role{}.Add(role)
	if errs != nil {
		tool.Error(c, 0, errs.Error())
		return
	}
	tool.Success(c, 200, "添加成功", "")
}

func (ctr RoleController) Edit(c *gin.Context) {

	id := c.DefaultQuery("id", "0")

	if ids, _ := strconv.Atoi(id); ids < 0 {
		tool.Error(c, 0, "id不正确")
		return
	} else {
		role, _ := mysql.Role{}.GetItemById(ids)
		fmt.Println(role)
		c.HTML(200, "admin/role/edit.html", gin.H{
			"item": role,
		})
	}
}

func (ctr RoleController) EditAjax(c *gin.Context) {

	role := mysql.Role{}

	if err := c.Bind(&role); err != nil {
		tool.Error(c, 0, err.Error())
		return
	}

	errs := mysql.Role{}.Save(role)

	if errs != nil {
		tool.Error(c, 0, errs.Error())
		return
	}
	tool.Success(c, 200, "修改成功", "")
}
func (ctr RoleController) Del(c *gin.Context) {

	id := c.DefaultQuery("id", "0")
	if ids, _ := strconv.Atoi(id); ids < 0 {
		tool.Error(c, 0, "id参数错误")
		return
	} else {

		errs := mysql.Role{}.Del(ids)
		if errs != nil {
			tool.Error(c, 0, errs.Error())
			return
		}
		tool.Success(c, 200, "删除成功", "")
	}

}
func (ctr RoleController) Status(c *gin.Context) {
	id := c.DefaultQuery("id", "0")
	status := c.DefaultQuery("status", "0")

	ids, err := strconv.Atoi(id)
	if ids < 0 {
		tool.Error(c, 0, err.Error())
		return
	}

	statuss, err := strconv.Atoi(status)
	if statuss < 0 {
		tool.Error(c, 0, err.Error())
		return
	}

	errs := mysql.Role{}.SetStatus(ids, statuss)

	if errs != nil {
		tool.Error(c, 0, errs.Error())
		return
	}

	tool.Success(c, 200, "操作成功", "")

}
func (ctr RoleController) Px(c *gin.Context) {

	id := c.DefaultQuery("id", "0")
	px := c.DefaultQuery("px", "0")

	ids, err := strconv.Atoi(id)
	if ids < 0 {
		tool.Error(c, 0, err.Error())
		return
	}

	pxs, err := strconv.Atoi(px)
	if pxs < 0 {
		tool.Error(c, 0, err.Error())
		return
	}

	errs := mysql.Role{}.Sort(ids, pxs)

	if errs != nil {
		tool.Error(c, 0, errs.Error())
		return
	}

	tool.Success(c, 200, "操作成功", "")
}
